#include "UIPlaySceneFallingSolo.h"
#include "AppDelegate.h"

UIFallingModePanel * UIFallingModePanel::create(Size size, int keycount)
{
	auto fmp = new UIFallingModePanel();
	fmp->initWithColor(THEMECOLORBA(0));
	fmp->size = size;
	fmp->changeWidthAndHeight(size.width, size.height);
	fmp->keycount = keycount;

	auto frameNode = DrawNode::create();
	fmp->addChild(frameNode);
	frameNode->drawLine(Point(0, 0), Point(0, size.height), TEXTCOLORFA(225));
	frameNode->drawLine(Point(size.width, 0), Point(size.width, size.height), TEXTCOLORFA(225));
	frameNode->drawSolidRect(Point(0, 0), Point(size.width, size.height / 8), TEXTCOLORFA(225));

	Rect clipRect = Rect(1, size.height / 8, size.width - 2, size.height);
	auto clipNode = ClippingRectangleNode::create(clipRect);
	clipNode->setName("panelClip");
	fmp->addChild(clipNode);
	auto layerGrad = LayerGradient::create(Color4B(0, 0, 0, 160), Color4B(0, 0, 0, 255));
	clipNode->addChild(layerGrad);
	auto layerNoteField = LayerColor::create(Color4B(0, 0, 0, 0), size.width, size.height * 7 / 8);
	UI::setNodeAnch(layerNoteField, ANCH_BL);
	layerNoteField->setPosition(Point(1, size.height / 8));
	layerNoteField->setName("panelNoteField");
	clipNode->addChild(layerNoteField);

	return fmp;
}

void UIFallingModePanel::setTrackWidth(float trackwidth, float trackwidths)
{
	trackWidth = trackwidth;
	trackWidthS = trackwidths == 0 ? trackWidth : trackwidths;
}

void UIFallingModePanel::createKeys()
{
	if (trackWidth == 0)
	{
		trackWidth = size.width / keycount;
		trackWidthS = trackWidth;
	}

	for (int i = 0; i < keycount; i++)
	{
		auto key = LayerColor::create(THEMECOLORBA(150), (i == 0 ? trackWidthS : trackWidth) - 4, size.height / 8 - 2);
		key->setPositionX(i == 0 ? 2 : trackWidthS + trackWidth*(i - 1) + 2);
		char name[6];
		sprintf_s(name, 6, "key%d", i);
		key->setName(name);
		addChild(key);
		auto trackLight = Sprite::create("image/GamePlay/FallingTrackLight.png");
		UI::setNodeAnch(trackLight, ANCH_MB);
		trackLight->setOpacity(180);
		trackLight->setPosition(Vec2((i == 0 ? trackWidthS*.5f : trackWidthS + trackWidth*(i - .5f)), size.height / 8));
		trackLight->setScale((i == 0 ? trackWidthS : trackWidth) / trackLight->getContentSize().width, 0);
		sprintf_s(name, 6, "tkl%d", i);
		trackLight->setName(name);
		addChild(trackLight);
	}
}

UIFallingModeHitJudgment * UIFallingModeHitJudgment::create(Size size)
{
	auto fmhj = new UIFallingModeHitJudgment();
	fmhj->initWithColor(THEMECOLORBA(0));
	fmhj->size = size;
	UI::setNodeAnch(fmhj, ANCH_M);
	fmhj->changeWidthAndHeight(size.width, size.height);

	auto underline = LayerColor::create(THEMECOLORBA(64), size.width, size.height*.1f);
	fmhj->addChild(underline);
	fmhj->underline = underline;

	auto ljug = Label::createWithTTF("", FONTA, size.height*.9f);
	ljug->enableOutline(TEXTCOLORB, 2);
	UI::setNodeAnch(ljug, ANCH_MR);
	ljug->setDimensions(size.width*.83f, size.height*.9f);
	ljug->setHorizontalAlignment(TextHAlignment::LEFT);
	ljug->setPosition(size.width*.915f, size.height*.55f);
	fmhj->addChild(ljug);
	fmhj->judgtext = ljug;

	auto lcmb = Label::createWithTTF("", FONTA, size.height*.9f);
	lcmb->enableOutline(TEXTCOLORB, 2);
	UI::setNodeAnch(lcmb, ANCH_ML);
	lcmb->setDimensions(size.width*.83f, size.height*.9f);
	lcmb->setHorizontalAlignment(TextHAlignment::RIGHT);
	lcmb->setPosition(size.width*.085f, size.height*.55f);
	fmhj->addChild(lcmb);
	fmhj->combotext = lcmb;

	return fmhj;
}

void UIFallingModeHitJudgment::setInfo(JUDGMENT judgment, string comboval)
{
	string judgstr = "";
	switch (judgment)
	{
		case DREAM:
			judgstr = "DREAM"; break;
		case GREAT:
			judgstr = "GREAT"; break;
		case GOOD:
			judgstr = "GOOD"; break;
		case POOR:
			judgstr = "POOR"; break;
		case BREAK:
			judgstr = "BREAK"; break;
		case LOADING:
			judgstr = "LOADING"; break;
		default:
			break;
	}
	judgtext->setString(judgstr);
	combotext->setString(comboval);
	for (auto node : getChildren()) { node->stopAllActions(); }
	setLightEffect(judgment);
	playChangeEffect();
}

void UIFallingModeHitJudgment::setLightEffect(JUDGMENT judgment)
{
	Color3B txtcolors[4];
	auto settxtcolor = [&](int num, ...)
	{
		va_list c;
		va_start(c, num);
		int mod = num == 2 ? 2 : 1;
		for (int i = 0; i < 4; i++)
		{
			txtcolors[i] = i % mod == 0 ? va_arg(c, Color3B) : txtcolors[i - 1];
		}
		va_end(c);
	};
	Color3B ulcolor = Color3B::WHITE;
	int si = random(0, 3);
	switch (judgment)
	{
		case DREAM:
			settxtcolor(4, judgcolors[si % 4], judgcolors[(si + 1) % 4], judgcolors[(si + 2) % 4], judgcolors[(si + 3) % 4]);
			break;
		case GREAT:
			settxtcolor(2, judgcolors[4], judgcolors[0]);
			break;
		case GOOD:
			settxtcolor(2, judgcolors[5], judgcolors[0]);
			break;
		case POOR:
			settxtcolor(2, judgcolors[6], judgcolors[0]);
			ulcolor = judgcolors[6];
			break;
		case BREAK:
			settxtcolor(2, judgcolors[7], judgcolors[0]);
			ulcolor = judgcolors[7];
			break;
		case LOADING:
			settxtcolor(4, judgcolors[0], judgcolors[1], judgcolors[2], judgcolors[3]);
			break;
		default:
			break;
	}
	auto tinttime = .02f;
	auto txtact = RepeatForever::create(
		Sequence::create(
			TintTo::create(tinttime, txtcolors[0]),
			TintTo::create(tinttime, txtcolors[1]),
			TintTo::create(tinttime, txtcolors[2]),
			TintTo::create(tinttime, txtcolors[3]),
			NULL
		)
	);
	auto lineact = Sequence::create(
		Spawn::create(
			TintTo::create(0, ulcolor),
			FadeTo::create(0, 255),
			NULL
		),
		Spawn::create(
			TintTo::create(tinttime * 8, THEMECOLOR3B),
			FadeTo::create(tinttime * 8, 64),
			NULL
		),
		NULL
	);
	judgtext->runAction(txtact->clone());
	combotext->runAction(txtact->clone());
	underline->runAction(lineact);
}

void UIFallingModeHitJudgment::playChangeEffect()
{
	judgtext->runAction(EaseBackOut::create(
		Sequence::create(
			Spawn::create(
				ScaleTo::create(0, 1.1f, 1),
				SkewTo::create(0, -20, 0),
				NULL
			),
			Spawn::create(
				ScaleTo::create(.2f, 1),
				SkewTo::create(.2f, 0, 0),
				NULL
			),
			NULL
		)
	));
	combotext->runAction(EaseBackOut::create(
		Sequence::create(
			Spawn::create(
				ScaleTo::create(0, 1.1f, 1),
				SkewTo::create(0, 20, 0),
				NULL
			),
			Spawn::create(
				ScaleTo::create(.2f, 1),
				SkewTo::create(.2f, 0, 0),
				NULL
			),
			NULL
		)
	));
}
